/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BuildData;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

/**
 *
 * @author Administrator
 */
public final class LLMData {

    private HashMap<String, Integer> aswdAr = new HashMap<String, Integer>();
    private HashMap<String, Integer> seqnAr = new HashMap<String, Integer>();
    private HashMap<String, Integer> expnAr = new HashMap<String, Integer>();

//    public static void main(String argv[]) throws FileNotFoundException, XMLStreamException, UnsupportedEncodingException, IOException {
//        ASWD_EXPN_SEQN_Set aes = new ASWD_EXPN_SEQN_Set();
//    }

    public LLMData() throws FileNotFoundException, XMLStreamException, UnsupportedEncodingException, IOException {

        System.out.println("package BuildData; class LLMData");

        System.out.println("Đang thu thập dữ liệu huấn luyện mô hình ngôn ngữ chữ cái...");
        colectData();
        writeToFile();
    }

    public void colectData() throws FileNotFoundException, XMLStreamException {
        FileInputStream fis = new FileInputStream("trainset\\hand\\alpha_hand.xml");
        XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fis);

        while (xmlStreamReader.hasNext()) {
            int eventCode = xmlStreamReader.next();
            switch (eventCode) {
                case XMLStreamConstants.START_ELEMENT:
                    String localName = xmlStreamReader.getLocalName();
                    if(localName.equals("record")){
                        String recordId=xmlStreamReader.getAttributeValue(0);
                        System.out.println("record: "+recordId);
                    }
                    if (localName.equals("W")) {
                        String atValue = xmlStreamReader.getAttributeValue(0);
                        System.out.println(atValue);
                        String nsw = xmlStreamReader.getElementText();
                        try{
                        if (atValue.equals("ASWD")) {

                            if (aswdAr.containsKey(nsw)) {
                                Integer count = (Integer) aswdAr.get(nsw);
                                count++;
                                aswdAr.put(nsw, count);
                            } else {
                                aswdAr.put(nsw, 1);
                            }
                        }


                        if (atValue.equals("SEQN")) {
                            if (seqnAr.containsKey(nsw)) {
                                Integer count = (Integer) seqnAr.get(nsw);
                                count++;
                                seqnAr.put(nsw, count);
                            } else {
                                seqnAr.put(nsw, 1);
                            }

                        }

                        if (atValue.equals("EXPN")) {
                            if (expnAr.containsKey(nsw)) {
                                Integer count = (Integer) expnAr.get(nsw);
                                count++;
                                expnAr.put(nsw, count);
                            } else {
                                expnAr.put(nsw, 1);
                            }

                        }
                        }
                        catch(Exception e){
                            e.printStackTrace();
                        }

                        


                    }
            }
        }
        
        System.out.println("    +Số lượng ASWD: "+aswdAr.size());
        System.out.println("    +Số lượng EXPN: "+expnAr.size());
        System.out.println("    +Số lượng SEQN: "+seqnAr.size());

    }

    public void writeToFile() throws XMLStreamException, FileNotFoundException, UnsupportedEncodingException, IOException {
        BufferedWriter bwa = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("data\\llm\\aswd.db"), "UTF8"));
        BufferedWriter bwe = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("data\\llm\\expn.db"), "UTF8"));
        BufferedWriter bws = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("data\\llm\\seqn.db"), "UTF8"));
        ArrayList aswdL = new ArrayList(aswdAr.entrySet());
        Collections.sort(aswdL, new Comparator() {

            public int compare(Object o1, Object o2) {
                Map.Entry e1 = (Map.Entry) o1;
                Map.Entry e2 = (Map.Entry) o2;
                Integer first = (Integer) e1.getValue();
                Integer second = (Integer) e2.getValue();
                return second.compareTo(first);
            }
        });
        for (int i = 0; i < aswdL.size(); i++) {
            Map.Entry e = (Map.Entry) aswdL.get(i);
            String line = e.getKey() + " " + e.getValue() + "\n";
            bwa.write(line);
        }
        bwa.close();

        ArrayList expnL = new ArrayList(expnAr.entrySet());
        Collections.sort(expnL, new Comparator() {

            public int compare(Object o1, Object o2) {
                Map.Entry e1 = (Map.Entry) o1;
                Map.Entry e2 = (Map.Entry) o2;
                Integer first = (Integer) e1.getValue();
                Integer second = (Integer) e2.getValue();
                return second.compareTo(first);
            }
        });
        for (int i = 0; i < expnL.size(); i++) {
            Map.Entry e = (Map.Entry) expnL.get(i);
            String line = e.getKey() + " " + e.getValue() + "\n";
            bwe.write(line);
        }
        bwe.close();

        ArrayList seqnL = new ArrayList(seqnAr.entrySet());
        Collections.sort(seqnL, new Comparator() {

            public int compare(Object o1, Object o2) {
                Map.Entry e1 = (Map.Entry) o1;
                Map.Entry e2 = (Map.Entry) o2;
                Integer first = (Integer) e1.getValue();
                Integer second = (Integer) e2.getValue();
                return second.compareTo(first);
            }
        });
        for (int i = 0; i < seqnL.size(); i++) {
            Map.Entry e = (Map.Entry) seqnL.get(i);
            String line = e.getKey() + " " + e.getValue() + "\n";
            bws.write(line);
        }
        
        System.out.println("Mo hinh ngon ngu xay dung duoc");
        System.out.println("    +aswd: "+aswdL.size());
        System.out.println("    +expn: "+expnL.size());
        System.out.println("    +seqn: "+seqnL.size());
        bws.close();
    }

    public HashMap getAswdAr() {
        return aswdAr;

    }

    public HashMap getSeqnAr() {
        return seqnAr;
    }

    public HashMap getExpnAr() {
        return expnAr;
    }
}
